package cn.lcvc.xnc.application.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.lcvc.xnc.application.common.exception.model.MyWebException;
import cn.lcvc.xnc.application.dao.WasteDisposalMappingMapper;
import cn.lcvc.xnc.application.dao.WasteDisposalMappingPictureMapper;
import cn.lcvc.xnc.application.dao.WasteDisposalSiteMapper;
import cn.lcvc.xnc.application.model.WasteDisposalMapping;
import cn.lcvc.xnc.application.model.WasteDisposalMappingPicture;
import cn.lcvc.xnc.application.model.WasteDisposalSite;
import cn.lcvc.xnc.application.model.WasteDisposalSiteVirescencePicture;
import cn.lcvc.xnc.application.model.query.WasteDisposalMappingQuery;
import cn.lcvc.xnc.application.service.WasteDisposalMappingPictureService;
import cn.lcvc.xnc.application.service.WasteDisposalMappingService;
import cn.lcvc.xnc.application.service.WasteDisposalSiteService;
import cn.lcvc.xnc.authority.service.impl.IBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;


/**
 * @description 测绘管理的业务层接口
 * @author 王秋林
 * @date 2022-8-07 16:00
 */
@Service
@Slf4j
public class IWasteDisposalMappingService extends IBaseService<WasteDisposalMapping, WasteDisposalMappingQuery> implements WasteDisposalMappingService {

    @Autowired
    private WasteDisposalMappingMapper mapper;

    @Autowired
    private WasteDisposalSiteMapper wasteDisposalSiteMapper;

    @Autowired
    private WasteDisposalMappingPictureMapper wasteDisposalMappingPictureMapper;

    @Autowired
    private WasteDisposalMappingPictureService wasteDisposalMappingPictureService;

    @Override
    protected void beforeGet(Serializable id) {
        if(mapper.findById(id)==null){
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeUpdate(WasteDisposalMapping wasteDisposalMapping) throws MyWebException {
        if(wasteDisposalMapping.getId()==null){
            throw new MyWebException("编辑失败：id不能为空");
        }
        if(mapper.findById(wasteDisposalMapping.getId())==null){
            throw new MyWebException("编辑失败：找不到指定记录");
        }
        wasteDisposalMappingPictureMapper.deleteByMappingId(wasteDisposalMapping.getId());
        wasteDisposalMappingPictureMapper.saveInfo(wasteDisposalMapping.getId(),wasteDisposalMapping.getDsmUrl(),wasteDisposalMapping.getPictureUrl(),wasteDisposalMapping.getRemake());
//        if(wasteDisposalMapping.getDsmUrl()!=null || wasteDisposalMapping.getPictureUrl()!= null || wasteDisposalMapping.getRemake() != null){
//            WasteDisposalMappingPicture wasteDisposalMappingPicture = new WasteDisposalMappingPicture();
//            wasteDisposalMappingPicture.setId(wasteDisposalMapping.getId());
//            wasteDisposalMappingPicture.setMappingId(wasteDisposalMapping.getId());
//            wasteDisposalMappingPicture.setPictureUrl(wasteDisposalMapping.getPictureUrl());
//            wasteDisposalMappingPicture.setDsmUrl(wasteDisposalMapping.getDsmUrl());
//            wasteDisposalMappingPicture.setRemake(wasteDisposalMapping.getRemake());
//            wasteDisposalMappingPictureMapper.update(wasteDisposalMappingPicture);
//        }else {
//            throw new MyWebException("编辑失败：测绘图片信息不能为空");
//        }
    }

    @Override
    protected void beforeSave(WasteDisposalMapping wasteDisposalMapping) {
        if (wasteDisposalSiteMapper.findById(wasteDisposalMapping.getSiteId()) ==null ){
            throw new MyWebException("添加失败：该消纳场不存在");
        }
//        wasteDisposalMapping.setSiteId(wasteDisposalSite.getId());
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
//        schoolMajorGroupTeacherMapper.deleteByMajorGroupIds(ids);
    }


    @Override
    protected void afterGet(WasteDisposalMapping wasteDisposalMapping) {
        //添加消纳场关联属性
        if (wasteDisposalMapping.getSiteId() != null) {
            //查找测绘管理所属消纳场
            WasteDisposalSite wasteDisposalSite = wasteDisposalSiteMapper.findById(wasteDisposalMapping.getSiteId());
            //赋值
            wasteDisposalMapping.setWasteDisposalSite(wasteDisposalSite);
        }
        if (wasteDisposalMapping.getId() != null) {
            //查找测绘管理对应的图片及DSM数字模型链接
            WasteDisposalMappingPicture wasteDisposalMappingPicture = wasteDisposalMappingPictureMapper.findByMappingId(wasteDisposalMapping.getId());
            //赋值
            wasteDisposalMapping.setWasteDisposalMappingPicture(wasteDisposalMappingPicture);
        }
    }

    @Override
    protected void afterFindAll(List<WasteDisposalMapping> list) {
        for (WasteDisposalMapping wasteDisposalMapping : list) {
            //添加消纳场关联属性
            if (wasteDisposalMapping.getSiteId() != null) {
                //查找测绘管理所属消纳场
                WasteDisposalSite wasteDisposalSite = wasteDisposalSiteMapper.findById(wasteDisposalMapping.getSiteId());
                //赋值
                wasteDisposalMapping.setWasteDisposalSite(wasteDisposalSite);
            }
            //添加测绘图片关联属性
            if (wasteDisposalMapping.getId() != null) {
                //查找测绘管理所属消纳场
                WasteDisposalMappingPicture wasteDisposalMappingPicture = wasteDisposalMappingPictureMapper.findByMappingId(wasteDisposalMapping.getId());
                //赋值
                wasteDisposalMapping.setWasteDisposalMappingPicture(wasteDisposalMappingPicture);
            }
        }
    }

    @Override
    protected void afterSave(WasteDisposalMapping wasteDisposalMapping) {
        //添加测绘管理图片表数据
//        List<String> list = wasteDisposalMapping.getPictureList();
//        for (String picUrl : list) {
            wasteDisposalMappingPictureMapper.saveInfo(wasteDisposalMapping.getId(), wasteDisposalMapping.getDsmUrl(),wasteDisposalMapping.getPictureUrl(), wasteDisposalMapping.getRemake());
//        }
    }
//    public List<String> uploadImgFile(Integer id, MultipartFile[] file, String remake) {
//        List<String> urlList =  new ArrayList<>();
//        for (MultipartFile files : file) {
//            if (files.isEmpty()) {
//                throw new MyWebException("请选择要上传的图片");
//            } else {
//                //图片保存位置
//                String filePath = "C:\\img\\";
//                //创建文件mulu
//                FileUtil.mkdir(filePath);
//                //获取文件名
//                String fileName = files.getOriginalFilename();
//                //获取文件扩展名
//                String extensionName = FileUtil.extName(fileName);
////                修改文件名为时间戳
//                fileName = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()) + "." + extensionName;
//                //获取文件类型
//                String fileType = files.getContentType();
//                if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpg")) {
//                    if (files.getSize() / 1000 > 10000) {
//                        throw new MyWebException("图片大小不能超过10M,请重新选择要上传的图片");
//                    }
//                    try {
//                        WasteDisposalMappingPicture pic = new WasteDisposalMappingPicture();
//                        pic.setMappingId(id);
//                        pic.setPictureUrl(fileName);
//                        pic.setRemake(remake);
//                        urlList.add(fileName);
//                        files.transferTo(new File(filePath + fileName));
//                    } catch (IOException e) {
//                        throw new MyWebException("上传失败：" + e.getMessage());
//                    }
//                } else {
//                    throw new MyWebException("图片格式错误");
//                }
//            }
//        }
//        return urlList;
//    }
}
